function applyColors(text) {
    let lines = text.split('\n');
    let colors = {};
    lines.forEach(line => {
        let info = line.split(':');
        if (info.length == 2) {
            colors[info[0].trim().toUpperCase()] = [info[1].trim()];
        }
    });
    presetColors = colors;
}

function readUiConfig() {
    const lines = uiColors.split('\n');

    lines.forEach(line => {
        if (line.includes('Color(0x') && line.includes('=')) {
            let preset = line.replace('const ', '').replace(';', '').replace(' ', '');
            let info = preset.split('=');
            let color = info[1].replace('Color(', '').replace(')', '').replace(' ', '');

            console.log(`${color}: [${info[0]}]`);
        }
    });
    // console.log(presetColors);
}
// readUiConfig();

function rgbaToHex(rgba, alpha = 1) {
    // if (presetColors.length == 0) {
    //     readUiConfig();
    // }

    const [r, g, b, a] = rgba.match(/\d+(?:\.\d+)?/g).map(Number);
    function componentToHex(c) {
        var hex = c.toString(16);
        return hex.length == 1 ? "0" + hex : hex;
    }
    let color = "0x" + componentToHex(Math.ceil(a * 255 * alpha)).toUpperCase()
        + componentToHex(r).toUpperCase()
        + componentToHex(g).toUpperCase()
        + componentToHex(b).toUpperCase();

    if (presetColors[color]) {
        return presetColors[color][0];
    }
    return `const Color(${color})`;
}

function genTextArb(content) {
    key = content.replace(/[\,\n、\.]/g, '');

    // 是否全数字
    if (/^\d+$/.test(key)) {
        return {
            arb: ``,
            ref: `"${key}"`
        };
    }

    // 将字符串按空格分割为单词数组
    const words = key.split(' ');

    // 只取前五个单词
    let truncatedWords = words.slice(0, 5);
    // 清除数字
    truncatedWords = truncatedWords.filter(word => !/\d/.test(word));

    // 将单词数组转换为小驼峰字符串
    let camelCase = truncatedWords.map((word, index) => {
        // 如果不是第一个单词，则转换为驼峰
        if (index !== 0) {
            return word.charAt(0).toUpperCase() + word.slice(1);
        }
        // 如果是第一个单词，则转换为小驼峰
        return word.charAt(0).toLowerCase() + word.slice(1);
    }).join('');
    camelCase = camelCase.replace('\'', '');

    return {
        arb: camelCase == '' ? null : `"${camelCase}": "${content}"`,
        ref: camelCase.length > 0 ? `LocaleStrings.instance.${camelCase}` : `'${content}'`
    };
}

function showToast(msg) {
    var m = document.createElement('div');
    m.innerHTML = msg;
    m.style.cssText = "width:10%; font-size: 14px;background:#000; opacity:0.8; height:auto;min-height: 30px; color:#fff; line-height:30px; text-align:center; border-radius:4px; position:fixed; top:10%; left:45%; z-index:999999;";
    document.body.appendChild(m);
    setTimeout(function () {
        var d = 0.5;
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
        m.style.opacity = '0';
        setTimeout(function () { document.body.removeChild(m) }, d * 1000);
    }, 1500);
}

function copyToClipboard(text) {
    var textarea = document.createElement('textarea');
    textarea.style.position = 'fixed';
    textarea.style.opacity = 0;
    textarea.value = text;
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand('copy');
    document.body.removeChild(textarea);
}

function formatDart(code) {
    // 移除空行
    code = code.replace(/\n\s*\n/g, "\n");
    // 移除所有缩进
    code = code.replace(/\n\s+/g, "\n");
    let lines = code.split("\n");
    let nextIndent = 4;
    for (let i = 0; i < lines.length; i++) {
        if (nextIndent > 0) {
            lines[i] = " ".repeat(nextIndent) + lines[i];
        }
        let line = lines[i];
        let startCount = sum(line, "(") * 4 + sum(line, "[") * 2;
        let endCount = sum(line, ")") * 4 + sum(line, "]") * 2;
        nextIndent = nextIndent + startCount - endCount;
    }
    return lines.join("\n");
}

function sum(line, char) {
    let sum = 0
    for (let key of line) {
        if (key == char) {
            sum++
        }
    }
    return sum;
}

function unit(size) {
    if (size == 0) {
        return '0';
    }
    let num = `${size.toFixed(1)}`.replace('.0', '');
    return `${num}${sizeSuffix}`
}

function textUnit(size) {
    if (size == 0) {
        return '0';
    }
    return `${size}${textSuffix}`
}

function num(num) {
    return `${num.toFixed(1)}`.replace('.0', '');
}